<?php
/**
 * ArchivePressLog class file
 *
 * This file contains the ArchivePressLog class
 * @author University of London Computer Centre
 * @version 1.4.0 - Release Candidate 2
 * @package ArchivePress
 */

/**
 * ArchivePressLog class
 * @package ArchivePress
 * @subpackage classes
 */
class ArchivePressLog {
    /**#@+
     * Constants
     */
    /**
     * Just a "on"
     */
	const LOG_ON		= "on";
    /**
     * Just a "off"
     */
	const LOG_OFF		= "off";
    /**
     * Kind of logging -> basic
     */
	const LOG_BASIC		= "basic";
    /**
     * Kind of logging -> debug
     */
	const LOG_DEBUG		= "debug";

    /**
     * global wp db class
     * @access private
     * @var Wpdb
     */
	private $wpdb;
    /**
     * the log database table
     * @access private
     * @var string
     */
	private $logTable;
    /**
     * log status: yes>log, no>don't log
     * @access private
     * @var string
     */
	private $logStatus	= self::LOG_ON;
    /**
     * type of log
     * @access private
     * @var string
     */
	private $logLevel	= self::LOG_BASIC;

    /**
     * init everything
     *
     * @global Wpdb $wpdb
     */
	function __construct($logTable,$logStatus=self::ON,$logLevel=self::LOG_BASIC){
		// init table (its name can be different from the standard one )
		$this->logTable		= $logTable;
		// Other inits
		$this->logStatus	= $logStatus;
		$this->logLevel		= $logLevel;

		// set global db connection (and class)
		global $wpdb;
		$this->wpdb = $wpdb;
	}

    /**
     * log into the database
     *
     * @param string $level The log level
     * @param string $message The log message
     */
	public function logIt($level,$message) {
		// only if the logging is enabled
		if($this->logStatus == self::LOG_ON){
			// only if the level is the one decided
			if(($level == self::LOG_BASIC) || ($this->logLevel == self::LOG_DEBUG) ) {
				$this->wpdb->query("INSERT INTO ".$this->logTable."
				(id,log_date,level,message)
				VALUES ('',FROM_UNIXTIME(".strtotime(date('Y-m-d H:i:s'))."),'".$this->wpdb->escape($level)."','".$this->wpdb->escape($message)."') ");
			}
		}
	}

    /**
     * start the log system
     */
	public function startLogging() {
		$this->logStatus = self::LOG_ON;
	}

    /**
     * stop logging
     */
	public function stopLogging() {
		$this->logStatus = self::LOG_OFF;
	}

    /**
     * switch the log level to basic
     */
	public function basic() {
		$this->logLevel = self::LOG_BASIC;
	}

    /**
     * switch the log level to DEBUG
     */
	public function debug() {
		$this->logLevel = self::LOG_DEBUG;
	}

    /**
     * get the total logs count
     */
    public function getTotalLogs() {
        return $this->wpdb->get_var($this->wpdb->prepare("SELECT COUNT(id) FROM ".$this->logTable.""));
    }

    /**
     * get the basic logs count
     */
    public function getBasicLogs() {
        return $this->wpdb->get_var($this->wpdb->prepare("SELECT COUNT(id) FROM ".$this->logTable." WHERE level = %s", self::LOG_BASIC));
    }

    /**
     * get the debug logs count
     */
    public function getDebugLogs() {
        return $this->wpdb->get_var($this->wpdb->prepare("SELECT COUNT(id) FROM ".$this->logTable." WHERE level = %s", self::LOG_DEBUG));
    }

    /**
     * get the filteref logs
     * @param string $level The level
     * @param string $search The search value inside the text
     * @param string $limit The sql LIMIT
     */
    public function getFilteredLogs($level = "", $search = "", $limit = "") {
        // apply filters
        if($level != "all") {
            $level = $this->wpdb->prepare(" AND level = %s", $level);
        } else {
            $level = "";
        }
        if($search != "") {
            $search = $this->wpdb->prepare(" AND message LIKE %s", "%".$search."%");
        }

        return $this->wpdb->get_results("SELECT * FROM ".$this->logTable." WHERE 1=1 ".$level.$search." ORDER BY log_date DESC ".$limit);
    }

    /**
     * delete a log
     * @param integer $id The log id
     */
    public function deleteLog($id) {
                $this->wpdb->query($this->wpdb->prepare("DELETE FROM ".$this->logTable."
                                                         WHERE id = %d",  (int)$id));
    }

    /**
     * delete all the logs
     */
    public function emptyLog() {
                $this->wpdb->query($this->wpdb->prepare("DELETE FROM ".$this->logTable.""));
    }
}
?>